import { game, sys } from "cc";
import { BUILD } from "cc/env";

export const USE_WEB_GPU: boolean = false;
export const USE_OVERSEA: boolean = false;

export function isCN(): boolean {
    if (sys.isNative) {
        return (sys.language ?? '').toLowerCase() === 'zh-cn';
    } else {
        return (navigator?.language ?? '').toLowerCase() === 'zh-cn';
    }
}

async function checkWebGPUEnv(): Promise<void> {
    if (USE_WEB_GPU) {
        const supported = await checkWebGPUSupport();
        if (supported) {
            console.log("WebGPU is supported by the user's hardware!");
        } else {
            const popup = document?.getElementById('popup');
            if (popup) popup.className = 'popup-visible';
        }
    }
}

async function checkWebGPUSupport(): Promise<boolean> {

    if (!('gpu' in navigator)) return false;

    try {
        const adapter = await (navigator.gpu as any).requestAdapter();
        if (!adapter) {
            return false;
        }
        const device = await adapter.requestDevice();
        return !!device;
    } catch (e) {
        console.error("Failed to create WebGPU device:", e);
        return false;
    }
}

if (BUILD && USE_WEB_GPU) checkWebGPUEnv();

if (BUILD && sys.isNative) {
    console.info("dawn frame rate on open-harmony");
    game.frameRate = 30;
}